import 'dart:io';

import 'package:flutter/material.dart';
import 'package:gold/app/user_provider.dart';
import 'package:gold/route/routes.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/utils/xfg_utils.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:provider/provider.dart';

/// @description: 实时行情页面
/// @author tangzhentao
/// @created at 09:59 2024/12/8

class RealTimeQuotesPage extends StatelessWidget {
  const RealTimeQuotesPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: xfgAppbar(title: '实时行情'),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
        child: Column(
          children: [
            Expanded(
              child: Container(
                margin: EdgeInsets.only(top: XfgSize().size10, bottom: XfgSize().size72),
                child: Stack(
                  children: [
                    Container(
                      height: XfgSize().size46,
                      padding: EdgeInsets.only(top: XfgSize().size10, left: XfgSize().size12),
                      decoration: BoxDecoration(color: XfgColor.homeTop, borderRadius: BorderRadius.circular(XfgSize().size6)),
                      alignment: Alignment.topLeft,
                      child: Builder(builder: (con) {
                        final date = con.select<UserProvider, GoldQuotes?>((k) => k.goldQuotes)?.date?? '';
                        final now = DateTime.now();
                        final isOpen = ((now.hour >= 0 && now.hour < 2) || (now.hour == 2 && now.minute <= 30))
                            || ((now.hour >= 9 && now.hour < 15) || (now.hour == 15 && now.minute <= 30))
                            || now.hour >= 20;
                        return XfgText('$date    ${isOpen ? '开盘':'闭盘'}', fontSize:  XfgSize().text14, color: XfgColor.homeRealPrice, height: Platform.isIOS ? 1:null);
                      }),
                    ),
                    Align(
                      alignment: Alignment.topCenter,
                      child: Container(
                        margin: EdgeInsets.only(top: XfgSize().size34),
                        decoration: BoxDecoration(color: XfgColor.white, borderRadius: BorderRadius.circular(XfgSize().size6)),
                        child: Column(
                          children: [
                            /// 标题
                            Container(
                              decoration: BoxDecoration(color: XfgColor.red.withValues(alpha: 0.2), borderRadius: BorderRadius.vertical(top: Radius.circular(XfgSize().size6))),
                              height: XfgSize().size44,
                              child: Row(
                                children: [
                                  Expanded(child: XfgText('商品', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold, textAlign: TextAlign.center)),
                                  Expanded(child: XfgText('回购', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold, textAlign: TextAlign.center)),
                                  Expanded(child: XfgText('销售', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold, textAlign: TextAlign.center)),
                                  Expanded(child: XfgText('高/低', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold, textAlign: TextAlign.center))
                                ],
                              ),
                            ),
                            Expanded(
                              child: Builder(builder: (con) {
                                final quotes = con.select<UserProvider, GoldQuotes?>((k) => k.goldQuotes)?.map;
                                final List<Widget> children = [];
                                quotes?.forEach((_, quote) {
                                  children.add(SizedBox(
                                    height: XfgSize().size56 + XfgSize().size0_5,
                                    child: Column(
                                      children: [
                                        Expanded(
                                          child: Row(
                                            children: [
                                              Expanded(child: Center(child: XfgText(quote.varietynm?? '', fontSize: XfgSize().text16, color: XfgColor.text1, bold: FontWeight.bold))),
                                              Expanded(child: Center(child: XfgText(quote.buyPrice?? '', fontSize: XfgSize().text14, color: XfgColor.text1))),
                                              Expanded(child: Center(child: XfgText(quote.sellPrice?? '', fontSize: XfgSize().text14, color: XfgColor.text1))),
                                              Expanded(child: Center(child: XfgText('${quote.highPrice?? ''}\n', fontSize: XfgSize().text14, color: XfgColor.red, children: [
                                                TextSpan(text: quote.lowPrice?? '', style: TextStyle(color: XfgColor.text1))
                                              ])))
                                            ],
                                          ),
                                        ),
                                        Padding(padding: EdgeInsets.symmetric(horizontal: XfgSize().size14), child: Container(height: XfgSize().size0_5, color: XfgColor.colorE))
                                      ],
                                    ),
                                  ));
                                });
                                return SingleChildScrollView(
                                  child: Column(
                                    children: children,
                                  ),
                                );
                              }),
                            ),
                            Container(
                              margin: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size32, XfgSize().size10, XfgSize().size20),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(
                                    child: TextButton(
                                      onPressed: () => toGoldRecovery(replace: true),
                                      style: xfgButtonStyle(Size(double.infinity, XfgSize().size45)),
                                      child: XfgText('我要卖金', fontSize: XfgSize().text16, color: XfgColor.white),
                                    ),
                                  ),
                                  SizedBox(width: XfgSize().size10),
                                  Expanded(
                                    child: TextButton(
                                      onPressed: () => XfgNav.pushReplacementNamed(Routes.goldInvest),
                                      style: xfgButtonStyle(Size(double.infinity, XfgSize().size45)),
                                      child: XfgText('我要买金', fontSize: XfgSize().text16, color: XfgColor.white),
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            XfgText(
              '声明：行情仅供参考\n服务热线：2571-25889544\n地址：浙江省杭州市拱墅区莫干山路192号',
              fontSize: XfgSize().text14,
              color: XfgColor.text1D,
              textAlign: TextAlign.center,
              height: 2,
            ),
            SizedBox(height: XfgSize().bottomBarHeight),
          ],
        ),
      ),
    );
  }

}
